package game.components.squares;

import game.components.Cordinent;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseListener;
import java.awt.geom.GeneralPath;
import javax.swing.JComponent;

public abstract class BasicSquare extends JComponent
{
	private static final long	serialVersionUID	= -6399069703092096629L;

	private Dimension			size;
	protected boolean			highlighted;
	protected boolean			movable;
	protected boolean			movableMultipleSquares;
	Cordinent					location;

	// 1==Spy
	// 11==Bomb
	// 0==Flag

	public BasicSquare(MouseListener l, Cordinent cordinent)
	{
		super();
		size = new Dimension(70, 70);
		this.SetMouseListener(l);
		location = cordinent;
	}

	protected void drawBorder(Graphics2D g2d)
	{
		g2d.setColor(Color.white);

		GeneralPath p = new GeneralPath();
		p.moveTo(0, 0);
		p.lineTo(this.getWidth() - 1, 0);
		p.lineTo(this.getWidth() - 1, this.getHeight() - 1);
		p.lineTo(0, this.getHeight() - 1);
		p.closePath();
		g2d.draw(p);
	}

	protected static Graphics2D getAntiAliasedGraphics(Graphics g)
	{
		if (g != null)
		{
			Graphics2D g2d = (Graphics2D) g;
			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			return g2d;
		}
		return null;
	}

	public int getHeight()
	{
		return size.height;
	}

	public Cordinent getBoardLocation()
	{
		return this.location;
	}

	@Override
	public Dimension getPreferredSize()
	{
		return size;
	}

	@Override
	public Dimension getMinimumSize()
	{
		return size;
	}

	@Override
	public Dimension getMaximumSize()
	{
		return size;
	}

	public boolean getMovable()
	{
		return movable;
	}

	public boolean getMovableMultipleSquares()
	{
		if (!movable)
		{
			return false;
		}
		else
		{
			return this.movableMultipleSquares;
		}
	}

	@Override
	public String getName()
	{
		return Integer.toString(this.getBoardXLocation()) + ","
				+ Integer.toString(this.getBoardYLocation());
	}

	public int getWidth()
	{
		return size.width;
	}

	public int getBoardXLocation()
	{
		return this.location.getX();
	}

	public int getBoardYLocation()
	{
		return this.location.getY();
	}

	public void highlight(Graphics2D g2d)
	{
		if (g2d != null)
		{

			Composite oldComp = g2d.getComposite();

			Composite alphaComp = AlphaComposite.getInstance(
					AlphaComposite.SRC_OVER, 0.5f);

			g2d.setComposite(alphaComp);

			g2d.setColor(Color.white);
			g2d.setStroke(new BasicStroke(8));

			GeneralPath p = new GeneralPath();
			p.moveTo(4, 4);
			p.lineTo(this.getWidth() - 4, 4);
			p.lineTo(this.getWidth() - 4, this.getHeight() - 4);
			p.lineTo(4, this.getHeight() - 4);
			p.closePath();
			g2d.draw(p);

			g2d.setComposite(oldComp);
			g2d.setStroke(new BasicStroke(1));
		}

	}

	public boolean isHighlighted()
	{
		return this.highlighted;
	}

	public void movePiece(Cordinent newLocation)
	{
		this.location = newLocation;
	}

	@Override
	public abstract void paintComponent(Graphics g);

	public void setHighlighted(boolean highlighted)
	{
		this.highlighted = highlighted;
	}

	public void SetMouseListener(MouseListener l)
	{
		this.addMouseListener(l);
	}

}
